package com.xmy.cultivate.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.LackCourseLog;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xmy.cultivate.respons.LackCourseLogRs;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 缺课记录 Mapper 接口
 * </p>
 *
 * @author hpiggy
 * @since 2023-07-28
 */
@Mapper
public interface LackCourseLogMapper extends BaseMapper<LackCourseLog> {
    /**
     * 查询缺课记录
     *
     * @param page
     * @param wrapper
     * @return
     */
    @Select("SELECT lack_course_log.*,grade.name AS gradeName,grade.course_id AS courseId,grade.course_type_id as class_type_id,getCourseTypeName(grade.course_type_id) as class_type_name,staff.name AS teacherName,grade.course_id,t3.name as subjects_name FROM lack_course_log LEFT JOIN grade ON grade.id=lack_course_log.grade_id LEFT JOIN staff ON staff.id=grade.teacher_id LEFT JOIN subjects as t3 on lack_course_log.subjects_id = t3.id  ${ew.customSqlSegment}")
    IPage<LackCourseLog> getLackCourseLog(Page<LackCourseLog> page, @Param(Constants.WRAPPER) Wrapper wrapper);

    /**
     * 修改安排状态
     *
     * @param isRepair 状态
     * @param repairId 补课表id
     * @param id       编号
     * @return
     */
    @Select("UPDATE lack_course_log SET is_repair=#{isRepair},repair_id=#{repairId} WHERE id=#{id}")
    Boolean updateRemarkBoolean(@Param("isRepair") Integer isRepair, @Param("repairId") Long repairId, @Param("id") Long id);


    @Select({
            "<script>",
                "SELECT",
                    "t01.* ",
                    "FROM",
                        "lack_course_log AS t01",
                    "INNER JOIN course_scheduling_always AS t02 ",
                    "WHERE",
                        "t01.`year_part` = ${yearPart} ",
                        "AND t01.`quarter_num` = ${quarterNum} ",
                        "AND t01.subjects_id = ${subjectsId} ",
                        "AND t01.lesson_type = 1 ",
                        "AND t01.student_id = ${studentId} ",
                        "AND t02.num = ${lessonNum} ",
                        "AND t01.deleted = 0",
            "</script>"
    })
    public abstract LackCourseLog getLackCoruseLogForStudent(@Param("yearPart") Integer yearPart,@Param("quarterNum") Integer quarterNum,@Param("studentId") Long studentId,@Param("subjectsId") Long subjectsId,@Param("lessonNum") Integer lessonNum);

    @Select("select count(id) as count from lack_course_log where year_part = ${yearPart} and quarter_num = ${quarterNum} and subjects_id= ${subjectsId} and student_id = ${studentId} and lesson_type = ${lessonType} and lesson_num = ${lessonNum} and deleted =0")
    public Integer getLackCourseForNum(@Param("yearPart") Integer yearPart,@Param("quarterNum") Integer quarterNum,@Param("studentId") Long studentId,@Param("subjectsId") Long subjectsId,@Param("lessonNum") Integer lessonNum,@Param("lessonType") Integer lessonType);

    @Select({
            "<script>",
            "SELECT * FROM ( ",
                "SELECT",
                "t1.id,",
                "t1.school_id,",
                "t1.lesson_type,",
                "t1.subjects_id,",
                "t1.grade_id,",
                "t1.student_id,",
                "t1.status01 AS `status`,",
                "t1.year_class_id,",
                "t2.course_type_id AS class_type_id,",
                "getCourseTypeName ( t2.course_type_id ) AS class_type_name,",
                "getSubjectsName ( t1.subjects_id ) AS subjects_name,",
                "t1.lack_type,",
    //            "t5.school_date,",
                "t2.`name` AS grade_name,",
                "t3.`name` AS teacher_name,",
                "t4.`name` AS student_name,",
                "GROUP_CONCAT( t1.lesson_num ) AS lesson_num ",
                "FROM (",
                    "SELECT *, ",
                    "IF( `status` = 2, 2, 0 ) AS status01 ",
                    "FROM lack_course_log",
                    "WHERE year_part = ${yearPart} AND quarter_num = ${quarterNum} AND deleted = 0 ",
                    "<when test='lackType!=null and lackType!=&apos;&apos;'>",
                        "<when test='lackType==0'>",
                            "AND lack_type = 0 ",
                        "</when>",
                        "<when test='lackType==1'>",
                            "AND lack_type = 1 ",
                        "</when>",
                        "<when test='lackType==2'>",
                            "AND lack_type = 2 ",
                        "</when>",
                        "<when test='lackType==3'>",
                            "AND `status` = 2 ",
                        "</when>",
                    "</when>",
                ") AS t1",
                "LEFT JOIN grade AS t2 ON t1.grade_id = t2.id",
                "LEFT JOIN staff AS t3 ON t2.teacher_id = t3.id",
                "LEFT JOIN student AS t4 ON t1.student_id = t4.id",
    //            "LEFT JOIN course_scheduling_detail AS t5 ON t1.scheduling_always_id = t5.id AND t5.deleted = 0 ",
                "WHERE 1=1",
                "<when test='schoolId!=null and schoolId!=&apos;&apos;'>",
                    "AND t1.school_id IN ( ${schoolId} ) ",
                "</when>",
                "<when test='yearClassId!=null and yearClassId!=&apos;&apos;'>",
                    "AND t1.year_class_id IN ( ${yearClassId} )",
                "</when>",
                "<when test='gradeId!=null and gradeId!=&apos;&apos;'>",
                    "AND t1.grade_id IN ( ${gradeId} )",
                "</when>",
                "<when test='subjectsId!=null and subjectsId!=&apos;&apos;'>",
                    "AND t2.subjects_id IN ( ${subjectsId} )",
                "</when>",
                "<when test='teacherId!=null and teacherId!=&apos;&apos;'>",
                    "AND t2.teacher_id IN ( ${teacherId} )",
                "</when>",
                "<when test='classTypeId!=null and classTypeId!=&apos;&apos;'>",
                    "AND t2.course_type_id IN ( ${classTypeId} )",
                "</when>",
                "<when test='studentName!=null and studentName!=&apos;&apos;'>",
                    "AND t4.`name` LIKE '%${studentName}%'",
                "</when>",
                "GROUP BY",
                "t1.student_id,",
                "t1.grade_id, ",
                "t1.status01, ",
                "t1.lack_type ",
                "ORDER BY",
                "t1.student_id,",
                "t1.grade_id",
            ") AS t ",
            "WHERE 1=1",
            "<when test='idStr!=null and idStr!=&apos;&apos;'>",
                "AND id IN ( ${idStr} )",
            "</when>",
            "</script>"
    })
    public abstract IPage<LackCourseLogRs> lackStudentList(IPage<?> page, @Param("yearPart") Integer yearPart, @Param("quarterNum") Integer quarterNum, @Param("schoolId") String schoolId, @Param("yearClassId") String yearClassId, @Param("gradeId") String gradeId, @Param("subjectsId") String subjectsId, @Param("teacherId") String teacherId, @Param("classTypeId") String classTypeId, @Param("lackType") String lackType, @Param("studentName") String studentName,@Param("idStr") String idStr);

    @Select("UPDATE lack_course_log SET lack_type=${lackType} WHERE grade_id=${gradeId} AND student_id=${studentId} AND year_part=${yearPart} AND quarter_num=${quarterNum}")
    Boolean updateLackTypeBoolean(@Param("lackType") Integer lackType, @Param("gradeId") Long gradeId, @Param("studentId") Long studentId, @Param("yearPart") Integer yearPart, @Param("quarterNum") Integer quarterNum);

    @Select("UPDATE lack_course_log SET `status` = 2 WHERE grade_id=${gradeId} AND student_id=${studentId} AND year_part=${yearPart} AND quarter_num=${quarterNum} AND is_repair <> 1 AND `deleted` = 0")
    Boolean cancelLackBoolean(@Param("gradeId") Long gradeId, @Param("studentId") Long studentId, @Param("yearPart") Integer yearPart, @Param("quarterNum") Integer quarterNum);

    @Select("UPDATE repair_course INNER JOIN ( SELECT * FROM lack_course_log WHERE grade_id=${gradeId} AND student_id=${studentId} AND year_part=${yearPart} AND quarter_num=${quarterNum} AND is_repair <> 1 AND `status` = 2 AND `deleted` = 0 ) AS lack_course_log ON repair_course.record_course_id = lack_course_log.id SET repair_course.deleted = 1 ")
    Boolean cancelRepairCourseBoolean(@Param("gradeId") Long gradeId, @Param("studentId") Long studentId, @Param("yearPart") Integer yearPart, @Param("quarterNum") Integer quarterNum);

    @Select("SELECT lack_type FROM lack_course_log WHERE student_id=${studentId} AND subjects_id=${subjectsId} AND lesson_type=${lessonType} AND year_part=${yearPart} AND quarter_num=${quarterNum} AND `deleted` = 0 ORDER BY lack_type LIMIT 1")
    public Integer getLackTypeOne(@Param("yearPart") Integer yearPart, @Param("quarterNum") Integer quarterNum, @Param("studentId") Long studentId, @Param("subjectsId") Long subjectsId, @Param("lessonType") Integer lessonType);

}
